/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.HibernateUtilZoologia;
import muhnes.sizoo.modelo.Sinonimia;
/**
 *
 * @author CALDERON
 */
public class SinonimiaDaoImpl implements SinonimiaDao {
     Session sesion;
    
    public SinonimiaDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Sinonimia sinonimia ) {
        Transaction transaccion = sesion.beginTransaction();
       sesion.save( sinonimia );
        transaccion.commit();
    }

        
    @Override
    public void guardarColeccion( List< Sinonimia > sinonimias ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( sinonimias );
        transaccion.commit();
    }

    @Override
    public void actualizar( Sinonimia sinonimia ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( sinonimia );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Sinonimia > sinonimias ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( sinonimias );
        transaccion.commit();
    }

    @Override
    public void eliminar( Sinonimia sinonimia ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( sinonimia );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Sinonimia > sinonimias ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( sinonimias );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Sinonimia sinonimia = buscarPorId( id );
        sesion.delete( sinonimia );
        transaccion.commit();
    }

    @Override
    public Sinonimia buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Sinonimia where codigosinonimia = " + id);
        Sinonimia sinonimia = (Sinonimia)consulta.uniqueResult();
        transaccion.commit();
        return sinonimia;
    }

    @Override
    public List< Sinonimia > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Sinonimia");
        List< Sinonimia > sinonimias = consulta.list();
        transaccion.commit();
        return sinonimias;
    }

}